import { BaseData } from "../../framework/base/BaseData";
import { LogMgr } from "../../framework/manager/LogMgr";
import { SDKMgr } from "../../framework/manager/SDKMgr";
import { TimeUtils } from "../../framework/utils/TimeUtils";
import { UIUtils } from "../../framework/utils/UIUtils";
import { GameConfig } from "../config/Config";


class Data {
    path: string = ""
    dayShareNum: number = 0;
    daylastShareTime: number = -1;
}
export class ShareData extends BaseData {
    protected ecrypt: boolean;
    data: Data;
    gameKey: string = GameConfig.AppCacheName + "shareData";
    private timeTotalStop: number = 15;
    createData(): Data {
        this.data = new Data();
        return this.data;
    }
    initData(isCreatre: boolean) {
        if (!isCreatre) {
            if (this.data.daylastShareTime != -1) {
                let isDay = TimeUtils.compareIsToday(this.data.daylastShareTime)
                if (!isDay) {
                    this.data.daylastShareTime = -1;
                    this.data.dayShareNum = 0;
                    this.saveData();
                }
            }
        }
    }
    /**是否需要录屏 */
    private needRecordVideo: boolean = true;
    /**录屏了多少时间 */
    private recordTime: number = 0;
    private canShare: boolean = false;
    private isStart: boolean = false;
    private isPause: boolean = false;
    private isStop: boolean = false
    /**開始录屏 */
    public startRecord() {
        if (this.needRecordVideo) {
            LogMgr.getInstance().debug("开始录屏")
            this.isStart = true;
            this.isPause = false;
            SDKMgr.getInstance().recordScreen();
            this.needRecordVideo = false;
            this.canShare = false;
            UIUtils.schedule(this.secondUpdate, this, 1)
            this.data.path = ""
            this.saveData();
        }
    }
    public playRecord() {
        if (!this.isStart) {
            this.startRecord();
        } else {
            if (this.isPause) {
                this.restartRecord();
            } else {
                this.pauseRecord();
            }
        }
    }
    public pauseRecord() {
        if (this.isStart) {
            SDKMgr.getInstance().pauseRecord();
            this.isPause = true;
            LogMgr.getInstance().debug("暂停录屏")
        }
    }
    public restartRecord() {
        if (this.isPause) {
            this.isPause = false;
            SDKMgr.getInstance().restartRecord();
            LogMgr.getInstance().debug("重新录屏")
        }
    }
    /**停止录屏 */
    public stopRecord(isShare: boolean, cb?: Function) {
        if (this.recordTime <= 0) {
            return
        }
        UIUtils.unSchedule(this.secondUpdate, this)
        this.isStart = false;
        this.isPause = false;
        SDKMgr.getInstance().stopRecord(isShare, (path: string) => {
            this.data.path = path;
            if (this.recordTime > this.timeTotalStop) {
                this.canShare = true;
            }
            this.isStop = true;
            this.recordTime = 0;

            this.saveData();
            if (cb) {
                cb();
            }
        })
    }
    public getSharePath(): string {
        return this.data.path;
    }
    /**是否可以分享 */
    public getCanShare(): boolean {
        return this.recordTime > this.timeTotalStop || this.canShare
    }
    /**获取录屏时间 */
    public getRecordTime(): number {
        return this.recordTime;
    }
    /**分享 */
    public toShare(needAdd: boolean, callback: Function) {
        SDKMgr.getInstance().shareVideo(this.data.path, () => {
            this.data.path = "";
            if (needAdd) {
                this.data.dayShareNum++;
            }
            this.needRecordVideo = true;
            this.canShare = false;
            this.isStop = false
            this.clearData()
            this.saveData();
            if (callback) {
                callback();
            }
        })
    }
    /**获取今天是否可以分享得提示 */
    public getTodayCanShare(): boolean {
        // if (this.data.dayShareNum == 0) {
        //     return true;
        // }
        // let maxDay = DataMgr.getInstance().getParamCfgById(ParamsType.day_share_num)
        // if (this.data.dayShareNum < maxDay) {
        //     return true;
        // }
        return true;
    }
    /**每一秒倒计时 */
    secondUpdate() {
        if (this.isPause) {
            return;
        }
        this.recordTime++
        if (this.recordTime >= 290) {
            this.stopRecord(false)
        }
    }
    /**尝试去停止录屏 用于关卡时间比较短的情况 */
    checkStop(cb?: Function) {
        if (this.isStop) {
            if (cb) {
                cb()
            }
            return true;
        }
        //到最大时间后停止
        if (this.recordTime >= this.timeTotalStop || this.recordTime >= 290) {
            this.stopRecord(false, cb)
            return true
        }
        return false
    }
    isStopRecord(cb?: Function) {
        if (this.isStop) {
            if (cb) {
                cb()
            }
            return true;
        }
        return false;
    }
    /**手动清除数据 前提调stop */
    public clearData() {
        this.isStop = false
        this.isStart = false;
        this.needRecordVideo = true;
        this.data.path = "";
        this.recordTime = 0;
        this.saveData()
    }
}